package com.lenovo.manufacture;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LightingControl extends AppCompatActivity {
    private Button btOpen;
    private Button btClose;
    private Button btBack;
    private ImageView ivLamp;
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/UserFactoryEnvironmentalLog/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lightingcontrol);
        initview();
        lampClick();
    }

    private void initview() {
        btOpen=findViewById(R.id.bt_open);
        btClose=findViewById(R.id.bt_close);
        ivLamp=findViewById(R.id.iv_lamp);
        btBack=findViewById(R.id.bt_back);
    }

    private void lampClick(){
        btOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                control(1);
            }
        });
        btClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                control(0);
            }
        });
        btBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
    private void control(int i){
        Service service=retrofit.create(Service.class);
        Call<LampData> call=service.lamp(i);
        call.enqueue(new Callback<LampData>() {
            @Override
            public void onResponse(Call<LampData> call, Response<LampData> response) {
                int islighting=response.body().getData().get(0).getIsLighting();
                    if (islighting==0){
                        ivLamp.setImageResource(R.mipmap.lampno);
                    }else {
                        ivLamp.setImageResource(R.mipmap.lampyes);
                    }
            }

            @Override
            public void onFailure(Call<LampData> call, Throwable t) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}